<?php
App::uses('AdminAppController', 'Admin.Controller');
/**
 * AdminRegistersController
 *
 * @property Register $Register
 */
class AdminRegistersController extends AdminAppController {

	public $uses = array('Register');
    var $helpers = array('Admin.Csv');
    
    public function index() {
	    $conditions = array();
        $registersTableURL = array('controller' => 'admin_registers', 'action' => 'index');

        //join get query & named params
        $params = array_merge($this->request->params['named']);
        foreach($this->request->query as $key => $value) $params[$key] = $value;

        foreach($params as $key => $value) {
            $split = explode('-', $key);
            $modelName = (sizeof($split) > 1) ? $split[0] : 'Register';
            $property = (sizeof($split) > 1) ? $split[1] : $key;
            if($modelName == 'Register' || !empty($this->Register->belongsTo[$modelName])) {
                $this->loadModel($modelName);
                $modelObj = new $modelName();
                if(!empty($modelObj)) {
                    $columnType = $modelObj->getColumnType($property);
                    if(!empty($columnType)){
                        //add it to url
                        $registersTableURL[$key] = $value;
                        //add it to conditions
                        switch($columnType)
                        {
                            case 'string':
                                $conditions[$modelName . '.' . $property . ' LIKE'] = '%'.$value.'%';
                                break;
                            default:
                                $conditions[$modelName . '.' . $property] = $value;
                                break;
                        }
                    }
                }
            }

        }

        if($this->request->is('post')){
            $fullname = $this->request->data('fullname');
            $conditions['OR'] = array(
                    'fullname LIKE' => '%'.$fullname.'%', 
                    'email LIKE' => '%'.$fullname.'%' 
                );
                
            $this->set('fullname', $fullname);
        }
        
		$this->Register->recursive = 0;
		$this->paginate = array('conditions' => $conditions, 'limit' => ROW_PER_PAGE, 'order' => 'created DESC');
        $this->set('registers', $this->Paginator->paginate('Register'));
		$this->set('registersTableURL', $registersTableURL);
        $this->set('registersTableModelAlias', 'Register');
		//render as local table if it is an ajax request
        if($this->request->is('ajax'))
        {
            $this->render('table');
        }
	}
    
    public function view($id = null) {
        $this->Register->id = $id;
        if (!$this->Register->exists()) {
            throw new NotFoundException(__('Invalid register'));
        }
        $register = $this->Register->read(null, $id);
        $this->set('register', $register);
    }
    
	public function add() {
		if ($this->request->is('post')) {
			$this->Register->create();
			if ($this->Register->save($this->request->data)) {
				$this->Session->setFlash(__('The register has been saved'), 'default', array(), 'good');
                if(!empty($this->request->query['redirect'])) {
					$this->redirect($this->redirectUrl);
				} else {
					$this->redirect(array('action' => 'view', $this->Register->id));
				}
			} else {
				$this->Session->setFlash(__('The register could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		} else {
            //add the named params as data
            foreach($this->request->params['named'] as $param => $value) {
                $columnType = $this->Register->getColumnType($param);
                if(!empty($columnType)) {
                    if(empty($this->request->data['Register'])) $this->request->data['Register'] = array();
                    $this->request->data['Register'][$param] = $value;
                    //is this a reference to a related object?
                    foreach ($this->Register->belongsTo as $relationName => $relationInfo) {
                    	if($relationInfo['foreignKey'] == $param) {
                    		$relatedRecord = $this->Register->$relationInfo['className']->find('first', array('conditions' => array($relationInfo['className'] . '.id' => $value), 'recursive' => 0));
                    		$this->set(Inflector::variable($relationInfo['className']), $relatedRecord);
                    	}
                    }
                }
            }
        }
	}
	
	public function edit($id = null) {
		$this->Register->id = $id;
		if (!$this->Register->exists()) {
			throw new NotFoundException(__('Invalid register'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Register->save($this->request->data)) {
				$this->Session->setFlash(__('The register has been saved'), 'default', array(), 'good');
                $this->redirect($this->redirectUrl);
			} else {
				$this->Session->setFlash(__('The register could not be saved. Please, try again.'), 'default', array(), 'bad');
			}
		} else {
            $register = $this->Register->read(null, $id);
			$this->request->data = $register;
		}
	}
	
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Register->id = $id;
		if (!$this->Register->exists()) {
			throw new NotFoundException(__('Invalid register'));
		}
		if ($this->Register->delete()) {
			$this->Session->setFlash(__('Register deleted'), 'default', array(), 'good');
            $this->redirect($this->redirectUrl);
		}
		$this->Session->setFlash(__('Register was not deleted'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}

    function export()
    {
        $this->set('registers', $this->Register->find('all', array('fields' => array('`id` AS `No.`','fullname','email','birthday','phone','code','created'), 'order' => 'created DESC')));

        $this->layout = null;
        $this->autoLayout = false;

        Configure::write('debug','0');
    }
}
